<template>

    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="仓库名称">
            <el-input v-model="formInline.StorehouseName" clearable />
        </el-form-item>

        <el-form-item label="仓库编码">
            <el-input v-model="formInline.StorehouseCode" clearable />
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="StorehouseShow">查询</el-button>
            <el-button type="success" @click="StoreAdd">添加</el-button>
        </el-form-item>
    </el-form>

    <div style="border: 1px solid black; margin: 10px; padding: 10px;">
        <el-table :data="tableData" style="width: 100%">

            <el-table-column type="index" width="50" label="序号" />
            <el-table-column prop="storehouseName" label="仓库名称" width="100" />
            <el-table-column prop="storehouseCode" label="仓库编号" width="100" />
            <el-table-column prop="storehouseType" label="仓库类型" width="100" />
            <el-table-column prop="storehouseMember" label="仓管员" width="100" />
            <el-table-column prop="storehouseAddress" label="地址" width="100" />
            <el-table-column prop="storehouseMeg" label="描述" width="100" />
            <el-table-column prop="addProp" label="添加人" width="80" />
            <el-table-column prop="addTime" label="添加时间" width="180">
                <template #default="script">
                    {{ moment(script.row.addTime).format("YYYY-MM-DD HH:mm:ss") }}
                </template>
            </el-table-column>

            <el-table-column fixed="right" label="操作" min-width="120">
                <template #default="script">
                    <el-button link type="primary" size="small" @click="storeDel(script.row.id)">
                        删除
                    </el-button>
                    <el-button link type="primary" size="small">Edit</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>

    <!-- 添加弹框 -->
    <el-dialog v-model="storeAddVis" title="添加仓库" width="500">
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">

            <el-form-item label="仓库名称" prop="storehouseName">
                <el-input v-model="ruleForm.storehouseName" />
            </el-form-item>

            <el-form-item label="仓库编码" prop="storehouseCode">
                <el-input v-model="ruleForm.storehouseCode" />
            </el-form-item>

            <el-form-item label="仓库类型" prop="storehouseType">
                <el-input v-model="ruleForm.storehouseType" />
            </el-form-item>

            <el-form-item label="库管人" prop="storehouseMember">
                <el-input v-model="ruleForm.storehouseMember" />
            </el-form-item>
            <el-form-item label="地址" prop="storehouseAddress">
                <el-input v-model="ruleForm.storehouseAddress" />
            </el-form-item>
            <el-form-item label="描述" prop="storehouseMeg">
                <el-input v-model="ruleForm.storehouseMeg" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    添加
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>

        </el-form>

    </el-dialog>

</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import Mesaxios from '../../Http/MesHelp'
import moment from 'moment';
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

const formInline = reactive({
    StorehouseName: '',
    StorehouseCode: ''
})

const User = reactive({
    Name: localStorage.getItem("userNickname") || ''
})

//显示
const tableData = ref([])

const StorehouseShow = () => {
    Mesaxios.get("/api/Storehouse/StorehouseShow", {
        params: {
            StorehouseName: formInline.StorehouseName,
            StorehouseCode: formInline.StorehouseCode
        }
    }).then(res => {
        tableData.value = res.data.data.list
        console.log(tableData.value);
    })
}

//删除方法
const storeDel = (id: any) => {

    ElMessageBox.confirm(
        '您确认要删除吗?',
        '删除警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {

            Mesaxios.post("/api/Storehouse/StorehouseDel", { id: id }).then(res => {
                if (res.data.code == 200) {
                    ElMessage.success("删除成功")
                    StorehouseShow()
                }
                else {
                    ElMessage.error("删除失败")
                    StorehouseShow()
                }
            })

            // ElMessage({
            //     type: 'success',
            //     message: 'Delete completed',
            // })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '您取消了删除',
            })
        })
}

// 添加
const storeAddVis = ref(false)
const StoreAdd = () => {
    storeAddVis.value = true
}
interface RuleForm {
    storehouseName: string
    storehouseCode: string
    storehouseType: string
    storehouseMember: string
    storehouseAddress: string
    storehouseMeg: string
    addProp: any
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
    storehouseName: '',
    storehouseCode: '',
    storehouseType: '',
    storehouseMember: '',
    storehouseAddress: '',
    storehouseMeg: '',
    addProp: User.Name
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
    storehouseName: [
        { required: true, message: '仓库名称不能为空', trigger: 'blur' },
    ],
    storehouseCode: [
        { required: true, message: '仓库编码不能为空', trigger: 'blur' },
    ],
    storehouseType: [
        { required: true, message: '仓库类型不能为空', trigger: 'blur' },
    ],
    storehouseMember: [
        { required: true, message: '库管人不能为空', trigger: 'blur' },
    ],
    storehouseAddress: [
        { required: true, message: '仓库地址不能为空', trigger: 'blur' },
    ],
    storehouseMeg: [
        { required: true, message: '仓库描述不能为空', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')

            Mesaxios.post("/api/Storehouse/StorehouseAdd", ruleForm).then(res => {
                if (res.data.code == 200) {
                    ElMessage.success("添加储位成功")
                    storeAddVis.value = true
                    StorehouseShow();
                }
                else {
                    ElMessage.error("添加储位失败")
                    storeAddVis.value = true
                    StorehouseShow();
                }
            })

        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))



onMounted(() => {
    StorehouseShow();

})
</script>
